#ifndef CWIDGETBROWSER_H
#define CWIDGETBROWSER_H
#include <qglobal.h>
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))

#include <QWidget>
#include <QSslError>
#include <QWebEnginePage>

class QNetworkReply;
class QWebEngineView;
class QWebEngineDownloadItem;

class CWidgetBrowser : public QWidget
{
    Q_OBJECT

public:
    CWidgetBrowser(QWidget *parent = nullptr);
    ~CWidgetBrowser();

    /// 设置window对象，以便JS可以使用object的public槽函数\n
    bool SetWindowObject(QObject *object);

    // 地图上鼠标显示箭头\n
    void SetArrowToolMode(bool isArrowTool);

    // 设置无右键菜单\n
    void SetNoContextMenu();

    // 设置网页背景色\n
    void SetBackgroundColor(const QColor &color);

    // 返回webview
    QWebEngineView *WebView()
    {
        return m_pWebView;
    }

    QWebEnginePage *Page()
    {
        return m_pPage;
    }

Q_SIGNALS:
    void LoadStarted();
    void LoadFinished(bool);
    void EventTransit(QEvent *);

public Q_SLOTS:
    // 加载网页\n
    void LoadUrl(const QString &url);

    // 执行js脚本方法\n
    void InvokeScript(const QString &mothed, const QString &args);
    void InvokeScript(const QString &script);

    // 停止\n
    void Stop();

    // 后退\n
    bool CanGoBack();
    void GoBack();

    // 前进\n
    bool CanGoForward();
    void GoForward();

    // 删除cookie
    void DeleteCookie();

    // 刷新\n
    void Reload();
protected:
    virtual void closeEvent(QCloseEvent *event);

protected Q_SLOTS:
    void slot_sslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
    // void slot_unsupportedContent(QNetworkReply *);
    void slot_downloadRequested(QWebEngineDownloadItem *item);
    void slot_featurePermissionRequested(QUrl, QWebEnginePage::Feature);


private:
    QWebEngineView *m_pWebView{ nullptr };
    QWebEnginePage *m_pPage{ nullptr };
    QObject *m_pWindowObject{ nullptr };
};

#endif

#endif // !CWIDGETBROWSER_H
